#!/bin/bash

if [[ $# -eq 0 ]];then
  echo "#####################################################"
  echo "# podscale.sh [start|stop] [cs-dev|uat|tls-pre|...] #"
  echo "# start or stop  all pods in the environment        #"
  echo "#####################################################"
  exit 0
fi

#if [[ $2 == "tlsmt-pre" || $2 == "tls-pre" || $2 == "cs-pre" || $2 == "oms4-pre" ]];then
#   env=y3pre
#elif [[ $2 == "lova-qc" || $2 == "test" || $2 == "lova-test" || $2 == "fse-test" || $2 == "nfse-qc" || cs-dev ]];then
#   env=DQ
#elif [[ $2 == "cs-perf" || $2 == "lova-perf" || $2 == "oms4-perf" || $2 == "fse-uat" || $2 == "uat" || $2 == "demo" ]];then
#   env=117
#else 
#  echo "second args is error"
#  exit 1
#fi

for i in y3pre DQ 117
do
kubectl get ns -A --kubeconfig=/root/.kube/config-$i|awk '{print $1}'|grep -w ^$2$
          if [[ $? -eq 0 ]];then
            env=$i
          fi
done
if [[ ! -z $env ]];then
  echo "env="$env
else
  echo "second args is error"
  exit 1
fi

for i in `kubectl get deployments -n $2 --kubeconfig=/root/.kube/config-$env|awk '{print $1}'|awk 'NR>1'`
do
  if [[ $1 == "start" ]];then
      echo "$1 $env $2 $i"
      kubectl scale deployment $i --replicas=1 -n $2 --kubeconfig=/root/.kube/config-$env
  elif [[ $1 == "stop" ]];then
      echo "$1 $env $2 $i"
      kubectl scale deployment $i --replicas=0 -n $2 --kubeconfig=/root/.kube/config-$env
  else
      echo "first args is error"
      exit 1
  fi
done
